# 异常
# except：捕捉所有异常
try:
    file = open("ziyan.txt","r",encoding="utf-8")
except:
    print("文件出现异常，文件不存在")

# 捕获指定的异常
try:
    print(name)
except NameError as e:
    print("出现的变量未定义")
    print(e)

# 捕获多个 指定的异常
try:
    print(1 / 0)
except (NameError,ZeroDivisionError) as e:
    print("捕捉多个异常")
    print(e)

# 没有异常
try:
    print("123")
except (NameError,ZeroDivisionError) as e:
    print("捕捉多个异常")
    print(e)
else:
    print("没有异常")

# 无论出不出现异常，都会执行
try:
    print(1 / 0)
except (NameError,ZeroDivisionError) as e:
    print("捕捉多个异常")
    print(e)
else:
    print("没有异常")
finally:
    print("无论是否出现异常，都会出现")


# 异常的传递
def fun_1():
    print("fun1开始执行")
    print(1 / 0)
    print("fun1执行完毕")
def fun_2():
    print("fun2开始执行")
    fun_1()
    print("fun2执行完毕")
def main():
    print("main开始执行")
    try:
        fun_2()
    except ZeroDivisionError as e:
        print("异常的传递：", e)
    print("main执行完毕")
main()